package com.doit.zhaohang.handlerlunbotu;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

/**
 * Created by 赵航 on 2017/5/19.
 */

public class Adapter extends PagerAdapter {
    private Context mContext;
    private List<Integer> mList;

    public Adapter(Context context, List<Integer> arr) {
        this.mList = arr;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        /**1.设置imageview
         * 2.设置对应position的图片
         * 3.将imageview添加到container（注意添加到，不然是一片空白）
         * 4.返回imageview
         * */



        ImageView imageView = new ImageView(mContext);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        //设置布局
        imageView.setLayoutParams(params);

        //给imageview设置对应的图片

        imageView.setImageResource(mList.get(position));

        //一定要加入布局，不然会一片空白
        container.addView(imageView);

        //最后返回对象
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
       // super.destroyItem(container, position, object);
        container.removeView((View) object);
    }
}
